package com.example.gobang.config;

import com.example.gobang.API.ChatAPI;
import com.example.gobang.API.GameAPI;
import com.example.gobang.API.MatchAPI;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

@Configuration
/**
 * 注意： 让Spring 认识到webSocket , 一定要加上 @EnableWebSocket ！ ！ ！
 */
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    @Autowired
    MatchAPI matchAPI;
    @Resource
    GameAPI gameAPI;

    @Autowired
    ChatAPI chatAPI;
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
//        注册一个webSocket操作处理器
        registry.addHandler(matchAPI,"/findMatch")
//                将 httpSession 加入到 WebSocketSession
                .addInterceptors(new HttpSessionHandshakeInterceptor());

//        将游戏请求的WebSocket 注册到拦截器上 并将 httpSession 加入到 WebSocketSession
        registry.addHandler(gameAPI,"/game")
                .addInterceptors(new HttpSessionHandshakeInterceptor());
//        将聊天请求的WebSocket 注册到拦截器上 并将 httpSession 加入到 WebSocketSession
        registry.addHandler(chatAPI,"/chat")
                .addInterceptors(new HttpSessionHandshakeInterceptor());

    }
}
